/**
 * @module 推荐
 */
const loaderData = require('../util/loaderData');
const router = require('koa-router')()


// 加载列表数据
let infolist;
( async function (){
    await loaderData('list').then(data =>{
        infolist = JSON.parse(data) 
    })
})()


// 加载用户数据
let user;
( async function (){
    await loaderData('users').then(data =>{
      user = JSON.parse(data) 
    })
})()


function loaderRecommend(userId){
    let recommneds = [];
    let list = infolist[userId] || [];
    list.forEach(item =>{
        let { id, author, summary, createTime, imgs } = item;
        recommneds.push({
            id,
            author,
            summary,
            createTime,
            imgs,
            headPortrait: user.headPortrait,
        })
     })
    
    return recommneds
}



router.get('/', (ctx, next) =>{
    let userId = ctx.state.token.userId;
    ctx.body = {
        res: loaderRecommend(userId),
        msg: "OK",
        statusCode: 200
    };
})

module.exports = router

